#include "HQTextStream.h"
#include <QDebug>
#include <vector>

void InitializeFile(QString url)//初始化文件数据
{
    QFile file(url);
    file.open(QIODevice::WriteOnly|QIODevice::Text);//写入
    //打开
    QString Write = "";
    QTextStream qTextWrite(&file);
    qTextWrite << Write;
    //关闭
    file.close();
}
int GetFileTextLine(QString url)//获取文件行数
{
    QFile file(url);
    file.open(QIODevice::ReadOnly|QIODevice::Text);
    //打开
    int line = 0;
    QTextStream qTextRead(&file);
    while(!qTextRead.atEnd())
    {
        qTextRead.readLine();
        line++;
    }
    //关闭
    file.close();
    return line;
}
int GetFileTextlength(QString url,int line)//获取文件一行的数据数量
{
    QFile file(url);
    file.open(QIODevice::ReadOnly|QIODevice::Text);
    //打开
    int length = 1;
    QTextStream qTextRead(&file);
    for(int i = 1;i < line; i++)
    {
        qTextRead.readLine();
    }
    while(1)
    {
        QString thisRead = qTextRead.read(1);
        if(thisRead == "|")
        {
            length++;
        }else{
            if(thisRead == "\n" || qTextRead.atEnd())
            {
                file.close();
                return length;
            }
        }
    }
}
QString GetFileText(QString url)//读取文件内所有的内容,返回字符串
{
    QFile file(url);
    file.open(QIODevice::ReadOnly|QIODevice::Text);
    //打开
    QString read;
    QTextStream qTextRead(&file);
    read = qTextRead.readAll();
    //关闭
    file.close();

    return read;
}
QString GetFileText(QString url,int line)//读取文件内指定行内容,返回字符串
{
    QFile file(url);
    file.open(QIODevice::ReadOnly|QIODevice::Text);
    //打开
    QString read;
    QTextStream qTextRead(&file);
    for(int i = 0;i < line; i++)
    {
        read = qTextRead.readLine();
    }
    //关闭
    file.close();
    return read;
}
QString GetFileText(QString url,int line,int length)//读取文件内指定行中指定词内容,返回字符串
{
    QFile file(url);
    file.open(QIODevice::ReadOnly|QIODevice::Text);
    //打开
    QString read = "";
    QTextStream qTextRead(&file);
    for(int i = 1;i < line; i++)
    {
        qTextRead.readLine();
    }
    int i = 0;
    while(1)
    {
        QString thisRead = qTextRead.read(1);
        if(thisRead == "|" || (thisRead == "" || thisRead == "\n"))
        {
            i++;
            if(i >= length)
            {
                //关闭
                file.close();
                return read;
            }
            read = "";
        }else{
            read.append(thisRead);
        }
    }
}
void WriteFileTextAdd(QString url,QString date)//文件续写
{
    QFile file(url);
    file.open(QIODevice::Append|QIODevice::Text);//Append:不覆盖写入
    //打开
    QTextStream qTextWrite(&file);
    qTextWrite << date;
    //关闭
    file.close();
}
void WriteFileTextAdd(QString url,QString date,int line)//行插入
{
    line -= 1;
    QFile file(url);
    file.open(QIODevice::ReadOnly|QIODevice::Text);//读取
    //打开
    QTextStream qTextWrite(&file);
    QString headLine = "";QString endLine = "";
    for(int i = 0;i < line; i++)
    {
        headLine.append(qTextWrite.readLine());
        headLine.append("\n");
    }

    while(!qTextWrite.atEnd())
    {
        endLine.append(qTextWrite.readLine());
        endLine.append("\n");
    }
    file.close();
    file.open(QIODevice::WriteOnly|QIODevice::Text);//写入
    qTextWrite << headLine << date << "\n" << endLine;
    //关闭
    file.close();
}
void DeleteFileText(QString url,int line)//行删除
{
    QFile file(url);
    file.open(QIODevice::ReadOnly|QIODevice::Text);//读取
    //打开
    QTextStream qTextWrite(&file);
    QString headLine = "";QString endLine = "";
    for(int i = 1;i < line; i++)
    {
        headLine.append(GetFileText(url,i));
        headLine.append("\n");
    }
    for(int i = line + 1;i <= GetFileTextLine(url); i++)
    {
        endLine.append(GetFileText(url,i));
        endLine.append("\n");
    }
    file.close();
    file.open(QIODevice::WriteOnly|QIODevice::Text);//写入
    InitializeFile(url);
    WriteFileTextAdd(url,headLine);
    WriteFileTextAdd(url,endLine);
    //关闭
    file.close();
}
FileOffset Search(QString url,QString searchWord)//搜索每一位数据
{
    FileOffset rt;
    QFile file(url);
    file.open(QIODevice::ReadOnly|QIODevice::Text);//读取
    //打开
    QTextStream qTextRead(&file);
    int line = 1;
    int length = 1;
    QString nowWord = "";
    while(!qTextRead.atEnd())
    {
        QString now = qTextRead.read(1);
        if(now == "\n")
        {
            if(nowWord == searchWord)
            {
                file.close();
                rt.result = true;
                rt.length = length;
                rt.line = line;
                return rt;
            }else{
                line++;
                nowWord = "";
                length = 1;
            }
        }else{
            if(now == "|")
            {
                if(nowWord == searchWord)
                {
                    file.close();
                    rt.result = true;
                    rt.length = length;
                    rt.line = line;
                    return rt;
                }else{
                    nowWord = "";
                    length++;
                }
            }else{
                nowWord.append(now);
            }
        }
    }
    //关闭
    file.close();
    rt.result = false;
    rt.length = 0;
    rt.line = 0;
    return rt;
}
FileOffset Search(QString url,QString searchWord,int StartLine,int StartLength)//从起始坐标搜索每一位数据
{
    if(StartLine > GetFileTextLine(url))
    {
        StartLine = GetFileTextLine(url);
        qDebug()<<"行溢出,已修正";
    }
    if(StartLength > GetFileTextlength(url,StartLine))
    {
        StartLength = GetFileTextlength(url,StartLine);
        qDebug()<<"列溢出,已修正";
    }

    FileOffset rt;
    QFile file(url);
    file.open(QIODevice::ReadOnly|QIODevice::Text);//读取
    //打开
    QTextStream qTextRead(&file);
    int line = StartLine;
    int length = StartLength;
    for(int i = 1;i < StartLine; i++)
    {
        qTextRead.readLine();
    }
    int i = 1;
    while(i < StartLength)
    {
        if(qTextRead.read(1) == "|")
        {
            i++;
        }
    }
    QString nowWord = "";
    while(!qTextRead.atEnd())
    {
        QString now = qTextRead.read(1);
        if(now == "\n")
        {
            if(nowWord == searchWord)
            {
                file.close();
                rt.result = true;
                rt.length = length;
                rt.line = line;
                return rt;
            }else{
                line++;
                nowWord = "";
                length = 1;
            }
        }else{
            if(now == "|")
            {
                if(nowWord == searchWord)
                {
                    file.close();
                    rt.result = true;
                    rt.length = length;
                    rt.line = line;
                    return rt;
                }else{
                    nowWord = "";
                    length++;
                }
            }else{
                nowWord.append(now);
            }
        }
    }
    //关闭
    file.close();
    rt.result = false;
    rt.length = 0;
    rt.line = 0;
    return rt;
}
FileOffset Search(QString url,QString searchWord,int length)//指定列搜索
{
    FileOffset rt;
    rt.length = length;
    QFile file(url);
    file.open(QIODevice::ReadOnly|QIODevice::Text);//读取
    //打开
    QTextStream qTextRead(&file);
    int i = 1;
    while(!qTextRead.atEnd())
    {
        qTextRead.readLine();
        if(searchWord == GetFileText(url,i,length))
        {
            rt.result = true;
            rt.line = i;
            return rt;
        }
        i++;
    }
    //关闭
    file.close();
    rt.result = false;
    rt.length = 0;
    rt.line = 0;
    return rt;
}
Searchreturn FuzzySearch(QString url,QString searchWord)//模糊搜索,返回所有搜索结果,通过传参地址返回每个结果的文件地址
{
    Searchreturn returnDate;
    returnDate.many = 0;
    returnDate.result = false;
    for(int i = 1;i <= GetFileTextLine(url);i++)
    {
        for(int s = 1;s <= GetFileTextlength(url,i);s++)
        {
            if(GetFileText(url,i,s).contains(searchWord))
            {
                FileOffset in;
                in.line = i;
                in.length = s;
                in.result = true;
                returnDate.offset.push_back(in);
                returnDate.many++;
                returnDate.result = true;
            }
        }
    }
    return returnDate;
}
Searchreturn FuzzySearch(QString url,QString searchWord,int StartLine,int StartLength)//从某行某列开始模糊搜索,返回所有搜索结果,通过传参地址返回每个结果的文件地址
{
    Searchreturn returnDate;
    returnDate.many = 0;
    returnDate.result = false;
    for(int i = StartLine;i <= GetFileTextLine(url);i++)
    {
        for(int s = StartLength;s <= GetFileTextlength(url,i);s++)
        {
            if(GetFileText(url,i,s).contains(searchWord))
            {
                FileOffset in;
                in.line = i;
                in.length = s;
                in.result = true;
                returnDate.offset.push_back(in);
                returnDate.many++;
                returnDate.result = true;
            }
        }
    }
    return returnDate;
}
